'use strict';

/**
 * 统一响应中间件
 * 确保所有接口返回格式一致
 */
module.exports = () => {
  return async function responseMiddleware(ctx, next) {
    // 扩展ctx对象，添加统一响应方法
    ctx.success = function(data = null, message = '操作成功', code = 200) {
      ctx.body = {
        code,
        message,
        data,
      };
      ctx.status = code;
    };

    ctx.fail = function(message = '操作失败', code = 400, data = null) {
      ctx.body = {
        code,
        message,
        data,
      };
      ctx.status = code;
    };

    await next();
  };
};
